package com.caration.shell.cr3d;

import java.util.ArrayList;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Vector;

import android.os.Handler;
import android.os.HandlerThread;

public class GLHandler {
	private static ArrayList f = new ArrayList();
	private HashMap a;
	private HandlerThread mWidgetThread;
	private Handler mWidgetHandler;
	private boolean d;
	private Vector e;

	public GLHandler() {
		a = new HashMap();
		d = false;
		e = new Vector();
		mWidgetThread = new HandlerThread("WidgetManager Database Operating");
		mWidgetThread.start();
		mWidgetHandler = new Handler(mWidgetThread.getLooper());
	}

	public static void post(Runnable runnable) {
		SharedData.getGLView().post(runnable);
	}

	public static void postDelayed(Runnable runnable, long l) {
		SharedData.getGLView().postDelayed(runnable, l);
	}

	public static void queueEvent(Runnable runnable) {
		SharedData.getGLView().queueEvent(runnable);
	}

	public static void removeCallbacks(Runnable runnable) {
		SharedData.getGLView().removeCallbacks(runnable);
	}

	// public final void a() {
	// this.d = true;
	// Object localObject1;
	// if (this.e.size() > 0) {
	// localObject2 = this.e.elements();
	// while (((Enumeration) localObject2).hasMoreElements()) {
	// localObject1 = (RenderRunnable) ((Enumeration) localObject2)
	// .nextElement();
	// ((RenderRunnable) localObject1).frame = (-1 + ((RenderRunnable)
	// localObject1).frame);
	// if (((RenderRunnable) localObject1).frame > 0) {
	// if (!((RenderRunnable) localObject1).autoInvalidate)
	// continue;
	// a.f().h();
	// continue;
	// }
	// f.add(new y(this, (RenderRunnable) localObject1, false));
	// switch (((RenderRunnable) localObject1).type) {
	// default:
	// break;
	// case 0:
	// a((Runnable) localObject1);
	// break;
	// case 1:
	// ((RenderRunnable) localObject1).run();
	// break;
	// case 2:
	// d((Runnable) localObject1);
	// }
	// }
	// }
	// this.d = false;
	// Object localObject2 = f.iterator();
	// while (((Iterator) localObject2).hasNext()) {
	// localObject1 = (y) ((Iterator) localObject2).next();
	// if (localObject1 == null)
	// continue;
	// if (!((y) localObject1).b) {
	// this.e.removeElement(((y) localObject1).a);
	// continue;
	// }
	// this.e.addElement(((y) localObject1).a);
	// }
	// f.clear();
	// }

//	public final void a(RenderRunnable renderrunnable) {
//		if (!d)
//			e.addElement(renderrunnable);
//		else
//			f.add(new y(this, renderrunnable, true));
//		if (renderrunnable.autoInvalidate)
//			com.censivn.C3DEngine.a.f().h();
//	}
//
//	public final void b(RenderRunnable renderrunnable) {
//		if (!d)
//			e.removeElement(renderrunnable);
//		else
//			f.add(new y(this, renderrunnable, false));
//	}

	public final void b(final Runnable runnable, long delay) {
		Runnable r = new Runnable() {
			@Override
			public void run() {
				a.remove(runnable);
				queueEvent(runnable);
			}
		};
		a.put(runnable, r);
		SharedData.getGLView().postDelayed(r, delay);
	}

	public final void postDelayedAsync(Runnable runnable, long delay) {
		mWidgetHandler.postDelayed(runnable, delay);
	}

	public final void postAsync(Runnable runnable) {
		mWidgetHandler.post(runnable);
	}

	public final void removeFromMap(Runnable runnable) {
		Runnable runnable1 = (Runnable) a.get(runnable);
		if (runnable1 != null) {
			a.remove(runnable);
			SharedData.getGLView().removeCallbacks(runnable1);
		}
	}

	public final void removeAsync(Runnable runnable) {
		mWidgetHandler.removeCallbacks(runnable);
	}

}
